package hqu.com.sensorapp;

import androidx.appcompat.app.AppCompatActivity;


import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager mSensorManager;
    EditText etOrientation;
    EditText etMagnetic;
    EditText etTemperature;
    EditText etLight;
    EditText etPressure;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etOrientation = (EditText) findViewById(R.id.etOrientation);
        etMagnetic = (EditText) findViewById(R.id.etMagnetic);
        etTemperature = (EditText) findViewById(R.id.etTemperature);
        etLight = (EditText) findViewById(R.id.etLight);
        etPressure = (EditText) findViewById(R.id.etPressure);
        // 获取传感器模拟器的传感器管理服务
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE) ;
    }
    protected void onResume()	{
        super.onResume();
        // 为系统的加速度传感器注册监听器

        mSensorManager.registerListener((SensorEventListener) this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener((SensorEventListener) this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);
    }
    protected void onStop()	{
        // 取消注册
        mSensorManager.unregisterListener((SensorEventListener) this);
        super.onStop();
    }
    public void onSensorChanged (SensorEvent event)	{
        float[] values = event.values;
        int sensorType=event.sensor.getType();
        StringBuilder sb = null;
        switch (sensorType)
        {
            case Sensor.TYPE_ORIENTATION:
                sb=new StringBuilder();
                sb.append("绕z周转过的角度：");
                sb.append(values[0]);
                sb.append("\n绕x周转过的角度：");
                sb.append(values[1]);
                sb.append("\n绕y周转过的角度：");
                sb.append(values[2]);
                etOrientation.setText(sb.toString());
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                sb=new StringBuilder();
                sb.append("X轴上的磁场方向：");
                sb.append(values[0]);
                sb.append("\nY轴上的磁场方向：");
                sb.append(values[1]);
                sb.append("\nZ轴上的磁场方向：");
                sb.append(values[2]);
                etMagnetic.setText(sb.toString());
                break;
            case Sensor.TYPE_AMBIENT_TEMPERATURE:
                sb=new StringBuilder();
                sb.append("当前温度为：");
                sb.append(values[0]);
                etTemperature.setText(sb.toString());
                break;
            case Sensor.TYPE_LIGHT:
                sb=new StringBuilder();
                sb.append("当前光的强度为：");
                etLight.setText(sb.toString());
                break;
            case Sensor.TYPE_PRESSURE:
                sb=new StringBuilder();
                sb.append("当前压力为：");
                etPressure.setText(sb.toString());
                break;
        }
        // 当传感器精度改变时回调该方法。
    }
    public void onAccuracyChanged(Sensor sensor, int accuracy)	{
    }



}

